package com.juzipi.demo.service;

import com.alibaba.excel.EasyExcel;
import com.juzipi.demo.config.ExcelListener;
import com.juzipi.demo.pojo.Cat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author juzipi
 * @Date 2021/5/2 10:38
 * @Info
 */
@Service
public class CatService {

    @Autowired
    private ExcelListener excelListener;

    public void write() {
        ArrayList<Cat> cats = new ArrayList<>();
//        cats.forEach(cat -> cat.setId((long) Math.random()*100));
        Cat cat = new Cat();
        for (int i = 0; i < 10; i++) {
            cat.setAge(10);
            cat.setName("小黄");
            cat.setId((long) (Math.random() * 1000));
            cat.setVariety("土狗"+ (int) (Math.random() * 100));
            cat.setSex("雄");
            cats.add(cat);
        }

        String file = "D:\\SpringBootLearning\\我的模板\\cat.xlsx";

        EasyExcel.write(file, Cat.class).sheet("宠物的信息").doWrite(cats);


    }


    public void read() {
        String file = "D:\\SpringBootLearning\\我的模板\\cat.xlsx";
        EasyExcel.read(file, Cat.class, excelListener).sheet().doRead();

    }
}
